{% extends "base.html" %}
{% load humanize %}
{% load widget_tweaks %}
{% load static %}

{% block title %}
VulnerableCode Package Details - {{ package.purl }}
{% endblock %}

{% block content %}
<section class="section pt-0">
    {% include "package_search_box.html"%}
</section>

{% if package %}
<section class="section pt-0">
    <div class="details-container">
        <article class="panel is-info panel-header-only">
            <div class="panel-heading py-2 is-size-6">
                Package details: <span class="tag is-white custom">
                    {{ package.purl }}
                </span>
            </div>
        </article>

        <div class="tabs is-boxed" id="tabs">
            <ul>
                <li class="is-active" data-tab="essentials">
                    <a>
                        <span>Essentials</span>
                    </a>
                </li>
                <li data-tab="history">
                    <a>
                        <span>
                            History ({{ history|length }})
                        </span>
                    </a>
                </li>
            </ul>
        </div>

        <div id="tab-content">
            <div class="tab-div is-active" data-content="essentials">
                <div class="tab-nested-div mb-1 pb-0">
                        {% if affected_by_vulnerabilities|length != 0 %}
                        <div class="pl-3 pr-3 mb-5 floating-purl pb-0">
                            {% else %}
                        <div class="pl-3 pr-3 mb-5 floating-purl pb-2">
                            {% endif %}
                        <table class="table vcio-table width-100-pct mt-2">
                            <tbody>
                                <tr>
                                    <td class="two-col-left">
                                        <span
                                            class="has-tooltip-multiline has-tooltip-black has-tooltip-arrow has-tooltip-text-left"
                                            data-tooltip="The package url or purl is a URL string used to identify and locate a software package.">
                                            purl
                                        </span>
                                    </td>
                                    <td class="two-col-right">
                                        {{ fixed_package_details.purl.to_string }}
                                    </td>
                                </tr>
                                {% if package.is_ghost %}
                                <tr>
                                    <td class="two-col-left">
                                        Tags
                                    </td>
                                    <td class="two-col-right">
                                        <span
                                            class="tag is-warning is-hoverablem has-tooltip-multiline has-tooltip-black"
                                            data-tooltip="This package does not exist in the upstream package manager or its repository."
                                            style="margin-right: 8px;">
                                            Ghost
                                        </span>
                                    </td>
                                </tr>
                                {% endif %}
                            </tbody>
                        </table>
                    </div>
                    {% if affected_by_vulnerabilities|length != 0 %}

                    <div class="pl-3 pr-3 mb-6 non-vuln pb-0">
                        <table class="table vcio-table width-100-pct">
                            <tbody>
                                <tr>
                                    <td class="two-col-left">
                                        Next non-vulnerable version
                                    </td>
                                    <td class="two-col-right">
                                        {% if fixed_package_details.next_non_vulnerable.version %}
                                        <a href="/packages/{{ fixed_package_details.next_non_vulnerable }}?search={{ fixed_package_details.next_non_vulnerable }}"
                                            target="_self">{{ fixed_package_details.next_non_vulnerable.version }}</a>
                                        {% else %}
                                        <span class="emphasis-vulnerable">None.</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <td class="two-col-left">
                                        Latest non-vulnerable version
                                    </td>
                                    <td class="two-col-right">
                                        {% if fixed_package_details.latest_non_vulnerable.version %}
                                        <a href="/packages/{{ fixed_package_details.latest_non_vulnerable }}?search={{ fixed_package_details.latest_non_vulnerable }}"
                                            target="_self">{{ fixed_package_details.latest_non_vulnerable.version }}</a>
                                        {% else %}
                                        <span class="emphasis-vulnerable">None.</span>
                                        {% endif %}
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>

                    {% endif %}

                    <div class="content ml-3 mr-3 pb-1">
                        <div class="has-text-weight-bold ml-1 mb-0 pb-0">
                            <span class="affected-fixed">Vulnerabilities affecting</span> this package ({{ affected_by_vulnerabilities|length }})
                        </div>

                        <table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
                            <thead>
                                <tr>
                                    <th style="width: 200px;">Vulnerability</th>
                                    <th>Summary</th>
                                    <th style="width: 310px;"><span class="affected-fixed">Fixed by</span></th>
                                </tr>
                            </thead>

                            <tbody>
                                {% for vulnerability in affected_by_vulnerabilities %}
                                <tr>
                                    <td>
                                        <a href="{{ vulnerability.get_absolute_url }}" target="_self">{{vulnerability.vulnerability_id }}</a>
                                        <br />
                                        Aliases:
                                        <br />
                                        {% for alias in vulnerability.alias %}
                                        {% if alias.url %}
                                        <a href="{{ alias.url }}" target="_blank">{{ alias }}<i
                                                class="fa fa-external-link fa_link_custom"></i></a>
                                        <br />
                                        {% else %}
                                        {{ alias }}
                                        <br />
                                        {% endif %}
                                        {% endfor %}
                                    </td>
                                    <td style="word-wrap: break-word; word-break: break-word;">
                                        {{ vulnerability.summary }}
                                    </td>
                                    <td style="word-wrap: break-word; word-break: break-all;">
                                        {% if package.purl == fixed_package_details.purl.to_string %}
                                        {% for key, value in fixed_package_details.items %}
                                        {% if key == "vulnerabilities" %}
                                        {% for vuln in value %}
                                        {% if vuln.vulnerability.vulnerability_id == vulnerability.vulnerability_id %}
                                        {% if vuln.fixed_by_package_details is None %}
                                        <span class="emphasis-vulnerable">There are no reported <span
                                                class="affected-fixed">fixed by</span> versions.</span>
                                        {% else %}
                                        {% for fixed_pkg in vuln.fixed_by_package_details %}
                                            <section>
                                                {% if fixed_pkg.fixed_by_purl_vulnerabilities|length == 0 %}
                                                <a href="/packages/{{ fixed_pkg.fixed_by_purl }}?search={{ fixed_pkg.fixed_by_purl }}"
                                                    target="_self">{{ fixed_pkg.fixed_by_purl.version }}</a>
                                                <br />
                                                <span class="emphasis-not-vulnerable"><span class="affected-fixed">Affected
                                                        by</span> 0 other vulnerabilities.</span>
                                                {% else %}
                                                <a href="/packages/{{ fixed_pkg.fixed_by_purl }}?search={{ fixed_pkg.fixed_by_purl }}"
                                                    target="_self">{{ fixed_pkg.fixed_by_purl.version }}</a>
                                                {% if fixed_pkg.fixed_by_purl_vulnerabilities|length != 1 %}
                                                <br />
                                                <span class="emphasis-vulnerable"><span class="affected-fixed">Affected
                                                        by</span> {{ fixed_pkg.fixed_by_purl_vulnerabilities|length }} other
                                                    vulnerabilities.</span>
                                                {% else %}
                                                <br />
                                                <span class="emphasis-vulnerable"><span class="affected-fixed">Affected
                                                        by</span> {{ fixed_pkg.fixed_by_purl_vulnerabilities|length }} other
                                                    vulnerability.</span>
                                                {% endif %}
    
                                                <div class="dropdown is-hoverable has-text-weight-normal is-right">
                                                    <div class="dropdown-trigger">
                                                        <i
                                                            class="fa fa-question-circle ml-0 fa-sm has-background-white has-text-link"></i>
                                                    </div>
                                                    <div class="dropdown-menu dropdown-vuln-list-width" id="dropdown-menu4"
                                                        role="menu">
                                                        <div class="dropdown-content dropdown-instructions-box-shadow">
                                                            <div class="dropdown-item">
                                                                <div style="max-height: 200px; overflow-y: auto;">
                                                                    This version is <span class="affected-fixed">affected
                                                                        by</span> these other vulnerabilities:
                                                                    <div style="padding-top: 5px;">
                                                                        {% for fixed_by_vuln in fixed_pkg.fixed_by_purl_vulnerabilities %}
                                                                        <div>
                                                                            <a href="/vulnerabilities/{{ fixed_by_vuln.vulnerability_id }}"
                                                                                target="_self">{{
                                                                                fixed_by_vuln.vulnerability_id }}</a>
                                                                        </div>
                                                                        {% endfor %}
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                {% endif %}
                                            </section>
                                        {% endfor %}
                                        {% endif %}
                                        {% endif %}
                                        {% endfor %}
                                        {% endif %}
                                        {% endfor %}
                                        {% endif %}
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="3">
                                        <span class="emphasis-not-vulnerable">This package is not known to be <span
                                                class="affected-fixed">affected by</span> vulnerabilities.</span>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <div class="content ml-3 mr-3 pb-0 mb-0">
                        <div class="has-text-weight-bold ml-1 mb-0 pb-0">
                            <span class="affected-fixed">Vulnerabilities fixed by</span> this package ({{ fixing_vulnerabilities|length }})
                        </div>

                        <table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
                            <thead>
                                <tr>
                                    <th style="width: 200px;">Vulnerability</th>
                                    <th>Summary</th>
                                    <th style="width: 225px;">Aliases</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for vulnerability in fixing_vulnerabilities %}
                                <tr>
                                    <td>
                                        <a href="{{ vulnerability.get_absolute_url }}" target="_self">{{ vulnerability.vulnerability_id }}</a>
                                    </td>
                                    <td>
                                        {{ vulnerability.summary }}
                                    </td>
                                    <td>
                                        {% for alias in vulnerability.alias %}
                                        {% if alias.url %}
                                        <a href="{{ alias.url }}" target="_blank">{{ alias }}<i
                                                class="fa fa-external-link fa_link_custom"></i></a>
                                        <br />
                                        {% else %}
                                        {{ alias }}
                                        <br />
                                        {% endif %}
                                        {% endfor %}
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="3">
                                        This package is not known to <span class="affected-fixed">fix</span>
                                        vulnerabilities.
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>

                        <br />
                    </div>
                </div>
            </div>
            <div class="tab-div content" data-content="history">
                <table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
                    <thead>
                        <tr>
                            <th> <span
                                    class="has-tooltip-multiline has-tooltip-black has-tooltip-arrow has-tooltip-text-left"
                                    data-tooltip="The date that the package was reported to be fixing or affected by the vulnerability.">
                                    Date </span> </th>
                            <th>
                                <span
                                    class="has-tooltip-multiline has-tooltip-black has-tooltip-arrow has-tooltip-text-left"
                                    data-tooltip="The process that reported this package is fixing or affected by">
                                    Actor
                                </span>
                            </th>
                            <th> <span
                                    class="has-tooltip-multiline has-tooltip-black has-tooltip-arrow has-tooltip-text-left"
                                    data-tooltip="Fixing or Affected by"> Action </span> </th>
                            <th> <span
                                    class="has-tooltip-multiline has-tooltip-black has-tooltip-arrow has-tooltip-text-left"
                                    data-tooltip="Vulnerability fixed by or affecting the package."> Vulnerability </span>
                            </th>
                            <th>
                                <span
                                    class="has-tooltip-multiline has-tooltip-black has-tooltip-arrow has-tooltip-text-left"
                                    data-tooltip="The public service that published the advisory or related information."> Source </span>
                            </th>
                            <th>
                                <span
                                    class="has-tooltip-multiline has-tooltip-black has-tooltip-arrow has-tooltip-text-left"
                                    data-tooltip="The version of VulnerableCode that performed the action. ">
                                    VulnerableCode Version
                                </span>
                            </th>
                        </tr>
                    </thead>
                    {% for log in history %}
                    <tr>
                        <td class="is-break-word wrap-strings">{{ log.get_iso_time }}</td>
                        <td class="is-break-word wrap-strings">{{ log.actor_name }}</td>
                        <td class="is-break-word wrap-strings">{{ log.get_action_type_label }}</td>
                        <td class="is-break-word wrap-strings"> <a href="/vulnerabilities/{{log.related_vulnerability}}"
                                target="_self">{{ log.related_vulnerability }}</a></td>
                        <td class="is-break-word wrap-strings"> <a href="{{ log.source_url }}"
                                target="_blank">{{log.source_url }}</a></td>
                        <td class="is-break-word wrap-strings"> {{ log.software_version }} </td>
                    </tr>
                    {% endfor %}
                </table>
            </div>
        </div>
    </div>
</section>
<script src="{% static 'js/main.js' %}" crossorigin="anonymous"></script>
{% endif %}
{% endblock %}